package com.hongyue.dey05;

import java.io.File;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;

public class Demo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String nextLine;
        File file;
        while (true) {
            System.out.println("请输出文件路径");
            nextLine = sc.nextLine();
            file = new File(nextLine);
            if (file.exists()) {
                if (file.isFile()) {
                    System.out.println("您输入的是文件路径 请输入文件夹路径");
                } else {
                    break;
                }
            } else {
                System.out.println("您输入的文件路径不存在 请从新输入");
            }
        }
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        chazhao(map, file);
        Set<String> strings = map.keySet();
        for (String string : strings) {
            System.out.println(string + "文件出现了" + map.get(string) + "次");
        }
    }

    private static void chazhao(HashMap<String, Integer> map, File file) {
        File[] files = file.listFiles();
        if (files == null) {
            return;
        }
        for (File file1 : files) {
            if (file1.isFile()) {
                String[] split = file1.getName().split("\\.");
                String s = split[split.length - 1];
                if (map.containsKey(s)) {
                    map.put(s, map.get(s) + 1);
                } else {
                    map.put(s, 1);
                }
            } else {
                chazhao(map, file1);
            }
        }
    }
}
